/**
 * 
 */
package com.hson.demo.base.web.mvc;

import java.beans.PropertyEditorSupport;
import java.math.BigDecimal;

import org.springframework.util.StringUtils;

/**
 * @author Hungel.zou
 * @time 2015年7月23日 下午4:57:01
 */
public class BigDecimalEditor extends PropertyEditorSupport {

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
	 */
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		text = text.trim();
		if (!StringUtils.hasText(text)) {
			setValue(null);
			return;
		}
		try {
			setValue(new BigDecimal(text));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.beans.PropertyEditorSupport#getAsText()
	 */
	@Override
	public String getAsText() {
		BigDecimal value = (BigDecimal) getValue();
		if (value != null) {
			return String.valueOf(value.doubleValue());
		}
		return null;
	}

}
